package com.xiaoxu.intranetweb.mybatisplusSzappdb.controller.gypsumCompete;

import cn.dev33.satoken.stp.StpUtil;
import com.alibaba.fastjson.JSONObject;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.dao.UserInfoDao;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.entity.SysUser;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.entity.TGypsumPlan;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.service.impl.TGypsumPlanServiceImpl;
import com.xiaoxu.intranetweb.util.SzResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/gypsumPlan")
public class GypsumPlanController {
    @Autowired
    private TGypsumPlanServiceImpl gypsumPlanService;
    @Autowired
    private UserInfoDao userInfoDao;
    //获取所有石膏计划
    @PostMapping("/getAllGypsumPlan")
    public SzResult getAllGypsumCompetes(@RequestBody JSONObject jsonObject,HttpServletRequest request) {
        Integer page = jsonObject.getInteger("page");
        Integer pageSize = jsonObject.getInteger("pageSize");
        String startTime = jsonObject.getString("startTime");
        String endTime = jsonObject.getString("endTime");
        String pdtId = jsonObject.getString("pdtId");
        String token = request.getHeader("Authorization");
        String plantId = request.getHeader("Plant-Id");
        return gypsumPlanService.getAllGypsumPlan(page, pageSize,pdtId, startTime, endTime, plantId);
    }
    @PostMapping ("/addGypsumPlan")
    public SzResult addGypsumPlan(@RequestBody TGypsumPlan gypsumPlan, HttpServletRequest request) throws Exception {
        String token = request.getHeader("Authorization");
        SysUser user = userInfoDao.getUser(StpUtil.getLoginIdByToken(token).toString());
        String plantId = request.getHeader("Plant-Id");
        return gypsumPlanService.addGypsumPlan(gypsumPlan,user);
    }
    @PostMapping ("/updateGypsumPlan")
    public SzResult updateGypsumPlan(@RequestBody TGypsumPlan gypsumPlan, HttpServletRequest request) throws Exception {
        String token = request.getHeader("Authorization");
        SysUser user = userInfoDao.getUser(StpUtil.getLoginIdByToken(token).toString());
        String plantId = request.getHeader("Plant-Id");
        return gypsumPlanService.updateGypsumPlan(gypsumPlan,user);
    }


    @PostMapping ("/delGypsumPlan")
    public SzResult delGypsumPlan(@RequestBody JSONObject jsonObject, HttpServletRequest request) throws Exception {
        String id = jsonObject.getString("id");
        String token = request.getHeader("Authorization");
        String plantId = request.getHeader("Plant-Id");
        return gypsumPlanService.delGypsumPlan(id);
    }

}
